/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaintGUI.java
 *
 * Created on 30.03.2011, 20:23:32
 */
package figurenZeichnen;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Chris
 */
public class GrafikGUI extends javax.swing.JFrame {

    private int art1 = 0, art2 = 0;
    private Point mousePosition1, mousePosition2;
    private int x1, y1, x2, y2, hoeheK, breiteK;
    private Color color, colorBack;
    private boolean resize = false, deleteB = true;
    private Image bufferedImage;
    private boolean diagB = false, fächerB = false, kreisB = false, spiraleB = false;
    private boolean diagColor = false, fächerColor = false, kreisColor = false, spiraleColor = false;
    private Color diagColorC, fächerColorC, kreisColorC, spiraleColorC;

    /** Creates new form PaintGUI */
    public GrafikGUI() {
        initComponents();
        setTitle("Zeichenblatt");
        fTool.setVisible(true);
        fTool.pack();
        fTool.setTitle("Zeichenfunktionen");
        fColor.setTitle("erweiterte Farben");
        fColorBack.setTitle("erweiterte Farben");
        fSettings.setTitle("Einstellungen");
        pnPaint.requestFocus();
        this.setLocation(0, 141);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        fTool = new javax.swing.JFrame();
        pnButtons1 = new javax.swing.JPanel();
        cbVorgefFiguren = new javax.swing.JComboBox();
        cbZiehFiguren = new javax.swing.JComboBox();
        cbBehaltenÜberschreiben = new javax.swing.JComboBox();
        cbFarbe = new javax.swing.JComboBox();
        btErwColor = new javax.swing.JButton();
        btSettings = new javax.swing.JButton();
        btSave = new javax.swing.JButton();
        btZeichnen = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();
        fColor = new javax.swing.JFrame();
        ccZeichenFarbe = new javax.swing.JColorChooser();
        btSetColor = new javax.swing.JButton();
        fSettings = new javax.swing.JFrame();
        lbBlattW = new javax.swing.JLabel();
        lbBlattH = new javax.swing.JLabel();
        tfBlattW = new javax.swing.JTextField();
        tfBlattH = new javax.swing.JTextField();
        btSize = new javax.swing.JButton();
        btBackColor = new javax.swing.JButton();
        fColorBack = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        ccHintergrundfarbe = new javax.swing.JColorChooser();
        jPanel2 = new javax.swing.JPanel();
        btSetColorBack = new javax.swing.JButton();
        lbWarning = new javax.swing.JLabel();
        pnPaint = new javax.swing.JPanel();

        fTool.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                onFocusTool(evt);
            }
        });
        fTool.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                onShortcutTool(evt);
            }
        });

        pnButtons1.setBorder(javax.swing.BorderFactory.createTitledBorder("Zeichenfunktionen"));
        pnButtons1.setLayout(new java.awt.GridLayout(3, 3));

        cbVorgefFiguren.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diagonalen", "Fächer", "konz. Kreise", "Spirale" }));
        pnButtons1.add(cbVorgefFiguren);

        cbZiehFiguren.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kreis", "Rechteck", "Linie", "Dreieck" }));
        pnButtons1.add(cbZiehFiguren);

        cbBehaltenÜberschreiben.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "überschreiben", "behalten" }));
        pnButtons1.add(cbBehaltenÜberschreiben);

        cbFarbe.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Schwarz", "Blau", "Cyan", "Dunkel Grau", "Grau", "Grün", "Hell Grau", "Magenta", "Orange", "Pink", "Rot", "Weiß", "Gelb" }));
        cbFarbe.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                onSetColor(evt);
            }
        });
        pnButtons1.add(cbFarbe);

        btErwColor.setText("erw. Farben");
        btErwColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onErwFarben(evt);
            }
        });
        pnButtons1.add(btErwColor);

        btSettings.setText("Einstellungen");
        btSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOpenSettings(evt);
            }
        });
        pnButtons1.add(btSettings);

        btSave.setText("Speichern");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSave(evt);
            }
        });
        pnButtons1.add(btSave);

        btZeichnen.setText("Zeichnen");
        btZeichnen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onPaint(evt);
            }
        });
        pnButtons1.add(btZeichnen);

        btDelete.setText("Alles löschen");
        btDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onDelete(evt);
            }
        });
        pnButtons1.add(btDelete);

        fTool.getContentPane().add(pnButtons1, java.awt.BorderLayout.CENTER);

        fColor.setFocusable(false);
        fColor.getContentPane().add(ccZeichenFarbe, java.awt.BorderLayout.CENTER);

        btSetColor.setText("Farbe setzen");
        btSetColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setErwColor(evt);
            }
        });
        fColor.getContentPane().add(btSetColor, java.awt.BorderLayout.PAGE_END);

        fSettings.setFocusable(false);
        fSettings.getContentPane().setLayout(new java.awt.GridBagLayout());

        lbBlattW.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbBlattW.setText("Blattbreite:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(lbBlattW, gridBagConstraints);

        lbBlattH.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbBlattH.setText("Blatthöhe:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(lbBlattH, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(tfBlattW, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(tfBlattH, gridBagConstraints);

        btSize.setText("Größe ändern");
        btSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSetSize(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(btSize, gridBagConstraints);

        btBackColor.setText("Hintergrundfarbe");
        btBackColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSetBackColor(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fSettings.getContentPane().add(btBackColor, gridBagConstraints);

        fColorBack.setFocusable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 434, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 2, Short.MAX_VALUE)
                    .addComponent(ccHintergrundfarbe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 3, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 435, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 37, Short.MAX_VALUE)
                    .addComponent(ccHintergrundfarbe, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 38, Short.MAX_VALUE)))
        );

        fColorBack.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        btSetColorBack.setText("Farbe setzen");
        btSetColorBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSetColorBack(evt);
            }
        });
        jPanel2.add(btSetColorBack);

        lbWarning.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbWarning.setText("Achtung! Blatt wird überschrieben!");
        jPanel2.add(lbWarning);

        fColorBack.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                resized(evt);
            }
        });

        pnPaint.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                onkBegin(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                onkEnd(evt);
            }
        });
        pnPaint.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                resized(evt);
            }
        });
        pnPaint.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                onShortcuts(evt);
            }
        });

        javax.swing.GroupLayout pnPaintLayout = new javax.swing.GroupLayout(pnPaint);
        pnPaint.setLayout(pnPaintLayout);
        pnPaintLayout.setHorizontalGroup(
            pnPaintLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 570, Short.MAX_VALUE)
        );
        pnPaintLayout.setVerticalGroup(
            pnPaintLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        getContentPane().add(pnPaint, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onkBegin(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onkBegin
        mousePosition1 = pnPaint.getMousePosition();
        if (fTool.isVisible()) {
        } else {
            fTool.setVisible(true);
        }
    }//GEN-LAST:event_onkBegin

    private void onkEnd(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onkEnd
        mousePosition2 = pnPaint.getMousePosition();
        x1 = mousePosition1.x;
        y1 = mousePosition1.y;
        x2 = mousePosition2.x;
        y2 = mousePosition2.y;
        if (cbZiehFiguren.getSelectedItem().equals("Kreis")) {
            art2 = 1;
        } else if (cbZiehFiguren.getSelectedItem().equals("Rechteck")) {
            art2 = 2;
        } else if (cbZiehFiguren.getSelectedItem().equals("Linie")) {
            art2 = 3;
        } else if (cbZiehFiguren.getSelectedItem().equals("Dreieck")) {
            art2 = 4;
        }
        repaint();
    }//GEN-LAST:event_onkEnd

    private void onDelete(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onDelete
        delete();
}//GEN-LAST:event_onDelete

    private void onPaint(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onPaint
        //Zeichnen der vorgefertigten Figuren
        if (cbVorgefFiguren.getSelectedItem().equals("Diagonalen")) {
            diagB = true;
            repaint();
        } else if (cbVorgefFiguren.getSelectedItem().equals("Fächer")) {
            fächerB = true;
            repaint();
        } else if (cbVorgefFiguren.getSelectedItem().equals("konz. Kreise")) {
            kreisB = true;
            repaint();
        } else if (cbVorgefFiguren.getSelectedItem().equals("Spirale")) {
            spiraleB = true;
            repaint();
        }
}//GEN-LAST:event_onPaint

    private void onErwFarben(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onErwFarben
        //Frame für erweiterte Farben setzen
        fColor.setVisible(true);
        fColor.pack();
}//GEN-LAST:event_onErwFarben

    private void onSetColor(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_onSetColor
        //Standard Farbe setzen
        if (cbFarbe.getSelectedItem().equals("Schwarz")) {
            color = Color.BLACK;
        } else if (cbFarbe.getSelectedItem().equals("Blau")) {
            color = Color.BLUE;
        } else if (cbFarbe.getSelectedItem().equals("Cyan")) {
            color = Color.CYAN;
        } else if (cbFarbe.getSelectedItem().equals("Dunkel Grau")) {
            color = Color.DARK_GRAY;
        } else if (cbFarbe.getSelectedItem().equals("Grau")) {
            color = Color.GRAY;
        } else if (cbFarbe.getSelectedItem().equals("Grün")) {
            color = Color.GREEN;
        } else if (cbFarbe.getSelectedItem().equals("Hell Grau")) {
            color = Color.LIGHT_GRAY;
        } else if (cbFarbe.getSelectedItem().equals("Magenta")) {
            color = Color.MAGENTA;
        } else if (cbFarbe.getSelectedItem().equals("Orange")) {
            color = Color.ORANGE;
        } else if (cbFarbe.getSelectedItem().equals("Pink")) {
            color = Color.PINK;
        } else if (cbFarbe.getSelectedItem().equals("Rot")) {
            color = Color.RED;
        } else if (cbFarbe.getSelectedItem().equals("Weiß")) {
            color = Color.WHITE;
        } else if (cbFarbe.getSelectedItem().equals("Gelb")) {
            color = Color.YELLOW;
        }
    }//GEN-LAST:event_onSetColor

    private void setErwColor(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setErwColor
        //erweiterte Farben setzen
        color = ccZeichenFarbe.getColor();
        fColor.dispose();
    }//GEN-LAST:event_setErwColor

    private void onSave(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSave
       //Save-Methode aufrufen
        save();
    }//GEN-LAST:event_onSave

    private void onOpenSettings(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOpenSettings
        //Frame für Einstellungen öffnen
        fSettings.setVisible(true);
        fSettings.pack();
    }//GEN-LAST:event_onOpenSettings

    private void onSetBackColor(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSetBackColor
        //Frame für Hintergrundfarbe öffnen
        fColorBack.setVisible(true);
        fColorBack.pack();
    }//GEN-LAST:event_onSetBackColor

    private void btSetColorBack(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSetColorBack
        //Hintergrundfarbe setzen
        colorBack = ccHintergrundfarbe.getColor();
        fColorBack.dispose();
        pnPaint.setBackground(colorBack);
    }//GEN-LAST:event_btSetColorBack

    private void onSetSize(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSetSize
        //Größe verändern
        int breite = Integer.parseInt(tfBlattW.getText());
        int höhe = Integer.parseInt(tfBlattH.getText());
        this.setSize(breite, höhe);
        pnPaint.repaint();
    }//GEN-LAST:event_onSetSize

    private void resized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_resized
        art2 = 9;
        repaint();
    }//GEN-LAST:event_resized

    private void onShortcuts(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onShortcuts
        //KeyListener für Shortcuts (Zeichenblatt)
        if (evt.isControlDown() && evt.getKeyCode() == 83) {
            save();
        }
    }//GEN-LAST:event_onShortcuts

    private void onFocusTool(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_onFocusTool
        //Focus für Toolbox setzen
        fTool.requestFocus();
    }//GEN-LAST:event_onFocusTool

    private void onShortcutTool(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onShortcutTool
        //KeyListener für Shortcuts (Toolbox)
        if (evt.isControlDown() && evt.getKeyCode() == 83) {
            save();
        }
    }//GEN-LAST:event_onShortcutTool

    private void save() {
        //Save-Funktion
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "JPG & GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showSaveDialog(pnPaint);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File selectedFile = chooser.getSelectedFile();
                String fileName = chooser.getSelectedFile().getPath();
                Point start = pnPaint.getLocationOnScreen();
                int xB1 = start.x;
                int yB1 = start.y;
                int xB2 = pnPaint.getWidth();
                int yB2 = pnPaint.getHeight();
                Rectangle rectangle = new Rectangle(xB1, yB1, xB2, yB2);

                Robot robot = new Robot();
                BufferedImage image = robot.createScreenCapture(rectangle);
                File file = new File(fileName + ".jpg");
                ImageIO.write((RenderedImage) image, "jpg", file);


            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

        }

    }

    private void backUp() {
        try {
            Point start = pnPaint.getLocationOnScreen();
            int xB1 = start.x;
            int yB1 = start.y;
            int xB2 = pnPaint.getWidth();
            int yB2 = pnPaint.getHeight();
            Rectangle rectangle = new Rectangle(xB1, yB1, xB2, yB2);
            Robot robot = new Robot();
            bufferedImage = robot.createScreenCapture(rectangle);
        } catch (AWTException ex) {
            Logger.getLogger(GrafikGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void backUpHelp() {
        boolean tempDiag = diagB;
        boolean tempFächer = fächerB;
        boolean tempKreis = kreisB;
        boolean tempSpirale = spiraleB;
        diagB = false;
        fächerB = false;
        kreisB = false;
        spiraleB = false;
        Graphics b = pnPaint.getGraphics();
        b.dispose();
        super.paint(b);
        drawBackUp(b);
        diagB = tempDiag;
        fächerB = tempFächer;
        kreisB = tempKreis;
        spiraleB = tempSpirale;
    }

    private void delete() {
        if (cbBehaltenÜberschreiben.getSelectedItem().equals("behalten")) {
            deleteB = false;
        }
        cbBehaltenÜberschreiben.setSelectedItem("überschreiben");

        diagB = false;
        fächerB = false;
        kreisB = false;
        spiraleB = false;
        diagColor = false;
        fächerColor = false;
        kreisColor = false;
        spiraleColor = false;
        repaint();
        bufferedImage = null;
    }

    @Override
    public void paint(Graphics g1) {
        if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
            super.paint(g1);
            if (deleteB == false) {
                cbBehaltenÜberschreiben.setSelectedItem("behalten");
                deleteB = true;
            }
        }
        if (diagB == true) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
                fächerB = false;
                kreisB = false;
                spiraleB = false;
                diagColor = false;
                fächerColor = false;
                kreisColor = false;
                spiraleColor = false;
                bufferedImage = null;
            }
            Graphics g2 = pnPaint.getGraphics();
            //Diagonalen zeichnen
            drawDiagonal(g2);
            resize = false;

        }

        if (fächerB == true) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
                diagB = false;
                kreisB = false;
                spiraleB = false;
                diagColor = false;
                fächerColor = false;
                kreisColor = false;
                spiraleColor = false;
                bufferedImage = null;

            }
            Graphics g3 = pnPaint.getGraphics();
            //Fächer zeichnen
            drawFaecher(g3);
            resize = false;
        }
        if (kreisB == true) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
                diagB = false;
                fächerB = false;
                spiraleB = false;
                diagColor = false;
                fächerColor = false;
                kreisColor = false;
                spiraleColor = false;
                bufferedImage = null;
            }
            Graphics g4 = pnPaint.getGraphics();
            //Kreise zeichnen
            drawKreise(g4);
            resize = false;
        }
        if (spiraleB == true) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
                delete();
                diagB = false;
                fächerB = false;
                kreisB = false;
                diagColor = false;
                fächerColor = false;
                kreisColor = false;
                spiraleColor = false;
                bufferedImage = null;
            }
            Graphics g5 = pnPaint.getGraphics();
            //Spirale zeichnen
            drawSpirale(g5);
            resize = false;
        }
        if (art2 == 1) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
            }
            //gezogenen Kreis zeichnen
            Graphics g6 = pnPaint.getGraphics();
            if (diagB == true || fächerB == true || kreisB == true || spiraleB == true) {
                backUpHelp();
                drawZKreis(g6);
                backUp();
                repaint();
            } else {
                drawZKreis(g6);
                backUp();
            }
            art2 = 0;
            resize = false;
        } else if (art2 == 2) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);

            }
            Graphics g7 = pnPaint.getGraphics();
            //gezogenes Rechteck zeichnen
            drawZRechteck(g7);
            backUp();
            art2 = 0;
            resize = false;
        } else if (art2 == 3) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);

            }
            Graphics g8 = pnPaint.getGraphics();
            //gezogene Linie zeichnen
            drawZLinie(g8);
            backUp();
            art2 = 0;
            resize = false;
        } else if (art2 == 4) {
            if (cbBehaltenÜberschreiben.getSelectedItem().equals("überschreiben") && resize == false) {
                super.paint(g1);
            }
            Graphics g9 = pnPaint.getGraphics();
            //gezogenes Dreieck zeichnen
            drawDreieck(g9);
            backUp();
            art2 = 0;
            resize = false;
        } else if (art2 == 9) {
            super.paint(g1);
            Graphics g10 = pnPaint.getGraphics();
            drawBackUp(g10);
            art2 = 0;
        }

    }

    private void drawDiagonal(Graphics g2) {
        int breite = pnPaint.getWidth();
        int hoehe = pnPaint.getHeight();
        if (diagColor == false) {
            diagColorC = color;
            g2.setColor(color);
            diagColor = true;
        }
        g2.setColor(diagColorC);
        g2.drawLine(0, 0, breite, hoehe);
        g2.drawLine(0, hoehe, breite, 0);

    }

    private void drawFaecher(Graphics g3) {
        int hoehe = pnPaint.getHeight();
        int breite = pnPaint.getWidth();
        int hilfsH = 0;
        if (fächerColor == false) {
            fächerColorC = color;
            g3.setColor(color);
            fächerColor = true;
        }
        g3.setColor(fächerColorC);
        while (hilfsH <= hoehe) {
            g3.drawLine(0, 0, breite, hilfsH);
            hilfsH += 10;
        }

    }

    private void drawKreise(Graphics g4) {
        int hoehe = pnPaint.getHeight();
        int breite = pnPaint.getWidth();
        int breiteN = breite / 2 - 5;
        int hoeheN = hoehe / 2 - 5;
        int hoeheKreis = 10;
        if (kreisColor == false) {
            kreisColorC = color;
            g4.setColor(color);
            kreisColor = true;
        }
        g4.setColor(kreisColorC);

        while (hoeheKreis < hoehe) {
            g4.drawOval(breiteN, hoeheN, hoeheKreis, hoeheKreis);
            hoeheN -= 5;
            breiteN -= 5;
            hoeheKreis += 10;

        }

    }

    private void drawSpirale(Graphics g5) {
        int hoehe = pnPaint.getHeight();
        int breite = pnPaint.getWidth();
        int mittelpunktH = hoehe / 2;
        int mittelpunktB = breite / 2;
        int dir = 1;
        int tempH = 0;
        int tempB = 0;
        int plusZ = 5;
        if (spiraleColor == false) {
            spiraleColorC = color;
            g5.setColor(color);
            spiraleColor = true;
        }
        g5.setColor(spiraleColorC);
        while (mittelpunktH < hoehe && mittelpunktB < breite) {
            if (dir == 1) {
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                mittelpunktB += plusZ;
                g5.drawLine(tempB, tempH, mittelpunktB, mittelpunktH);
                dir = 2;
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                plusZ += 5;
            } else if (dir == 2) {
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                mittelpunktH -= plusZ;
                g5.drawLine(tempB, tempH, mittelpunktB, mittelpunktH);
                dir = 3;
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                plusZ += 5;
            } else if (dir == 3) {
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                mittelpunktB -= plusZ;
                g5.drawLine(tempB, tempH, mittelpunktB, mittelpunktH);
                dir = 4;
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                plusZ += 5;
            } else if (dir == 4) {
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                mittelpunktH += plusZ;
                g5.drawLine(tempB, tempH, mittelpunktB, mittelpunktH);
                dir = 1;
                tempB = mittelpunktB;
                tempH = mittelpunktH;
                plusZ += 5;
            }

        }

    }

    private void drawZKreis(Graphics g6) {
        g6.setColor(color);
        if (y2 < y1 && x2 < x1) {
            //Links oben
            int hoeheZK = y1 - y2;
            int breiteZK = x1 - x2;
            g6.drawOval(x2, y2, breiteZK, hoeheZK);
        } else if (x1 < x2 && y1 > y2) {
            //rechts oben
            int hoeheZK = y1 - y2;
            int breiteZK = x2 - x1;
            y1 = y1 - hoeheZK;
            x1 = x2 - breiteZK;
            g6.drawOval(x1, y1, breiteZK, hoeheZK);
        } else if (x1 < x2 && y1 < y2) {
            //rechts unten
            int hoeheZK = y2 - y1;
            int breiteZK = x2 - x1;
            g6.drawOval(x1, y1, breiteZK, hoeheZK);
        } else if (x1 > x2 && y1 < y2) {
            //links unten
            int hoeheZK = y2 - y1;
            int breiteZK = x1 - x2;
            x1 = x1 - breiteZK;
            g6.drawOval(x1, y1, breiteZK, hoeheZK);
        }
    }

    private void drawZRechteck(Graphics g7) {
        g7.setColor(color);
        if (y2 < y1 && x2 < x1) {
            //Links oben
            int hoeheZR = y1 - y2;
            int breiteZR = x1 - x2;
            g7.drawRect(x2, y2, breiteZR, hoeheZR);
        } else if (x1 < x2 && y1 > y2) {
            //rechts oben
            int hoeheZR = y1 - y2;
            int breiteZR = x2 - x1;
            y1 = y1 - hoeheZR;
            x1 = x2 - breiteZR;
            g7.drawRect(x1, y1, breiteZR, hoeheZR);
        } else if (x1 < x2 && y1 < y2) {
            //rechts unten
            int hoeheZR = y2 - y1;
            int breiteZR = x2 - x1;
            g7.drawRect(x1, y1, breiteZR, hoeheZR);
        } else if (x1 > x2 && y1 < y2) {
            //links unten
            int hoeheZR = y2 - y1;
            int breiteZR = x1 - x2;
            x1 = x1 - breiteZR;
            g7.drawRect(x1, y1, breiteZR, hoeheZR);
        }
    }

    private void drawZLinie(Graphics g8) {
        g8.setColor(color);
        g8.drawLine(x1, y1, x2, y2);
    }

    private void drawDreieck(Graphics g9) {
        g9.setColor(color);
        if (y2 < y1 && x2 < x1) {
            //Links oben
            int breiteD = x1 - x2;
            int hoeheD = y1 - y2;
            int spitze = x2 + breiteD / 2;
            int lineU = y2 + hoeheD;
            g9.drawLine(spitze, y2, x2, lineU);
            g9.drawLine(spitze, y2, x1, lineU);
            g9.drawLine(x2, lineU, x1, lineU);
        } else if (x1 < x2 && y1 > y2) {
            //rechts oben
            int breiteD = x2 - x1;
            int hoeheD = y1 - y2;
            int spitze = x1 + breiteD / 2;
            g9.drawLine(spitze, y2, x1, y1);
            g9.drawLine(spitze, y2, x1 + breiteD, y1);
            g9.drawLine(x1, y1, x1 + breiteD, y1);
        } else if (x1 < x2 && y1 < y2) {
            //rechts unten
            int breiteD = x2 - x1;
            int hoeheD = y2 - y1;
            int spitze = x1 + breiteD / 2;
            g9.drawLine(spitze, y1, x2 - breiteD, y2);
            g9.drawLine(spitze, y1, x2, y2);
            g9.drawLine(x2 - breiteD, y2, x2, y2);

        } else if (x1 > x2 && y1 < y2) {
            //links unten
            int breiteD = x1 - x2;
            int hoeheD = y2 - y1;
            int spitze = x1 - breiteD / 2;
            g9.drawLine(spitze, y1, x2, y2);
            g9.drawLine(spitze, y1, x2 + breiteD, y2);
            g9.drawLine(x2, y2, x2 + breiteD, y2);
        }
    }

    private void drawBackUp(Graphics g10) {
        g10.drawImage(bufferedImage, 0, 0, pnPaint);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GrafikGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBackColor;
    private javax.swing.JButton btDelete;
    private javax.swing.JButton btErwColor;
    private javax.swing.JButton btSave;
    private javax.swing.JButton btSetColor;
    private javax.swing.JButton btSetColorBack;
    private javax.swing.JButton btSettings;
    private javax.swing.JButton btSize;
    private javax.swing.JButton btZeichnen;
    private javax.swing.JComboBox cbBehaltenÜberschreiben;
    private javax.swing.JComboBox cbFarbe;
    private javax.swing.JComboBox cbVorgefFiguren;
    private javax.swing.JComboBox cbZiehFiguren;
    private javax.swing.JColorChooser ccHintergrundfarbe;
    private javax.swing.JColorChooser ccZeichenFarbe;
    private javax.swing.JFrame fColor;
    private javax.swing.JFrame fColorBack;
    private javax.swing.JFrame fSettings;
    private javax.swing.JFrame fTool;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbBlattH;
    private javax.swing.JLabel lbBlattW;
    private javax.swing.JLabel lbWarning;
    private javax.swing.JPanel pnButtons1;
    private javax.swing.JPanel pnPaint;
    private javax.swing.JTextField tfBlattH;
    private javax.swing.JTextField tfBlattW;
    // End of variables declaration//GEN-END:variables
}
